import { createRouter, createWebHashHistory } from "vue-router";
const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            redirect: '/main'    // 重定向到 main 页面
        },
        {
            path: '/login',
            component: () => import('../views/login/login.vue')
        },
        {
            path: '/main',
            redirect: '/home',
            component: () => import("../views/main/index.vue"),
            children: [
                {
                    path: '/home',
                    name: '首页',
                    component: () => import("../views/main/components/Home.vue")
                },
                {
                    path: '/dormitory_manger',
                    name: '宿管管理',
                    component: () => import("../views/main/components/DormitoryManger.vue")
                },
                {
                    path: '/building',
                    name: '宿舍楼管理',
                    component: () => import("../views/main/components/Building.vue")
                },
                {
                    path: '/student',
                    name: '学生管理',
                    component: () => import("../views/main/components/Student.vue")
                },
                {
                    path: '/dormitory',
                    name: '宿舍管理',
                    component: () => import("../views/main/components/Dormitory.vue")
                },
                {
                    path: '/later_record',
                    name: '晚归记录',
                    component: () => import("../views/main/components/LaterRecord.vue")
                },
                {
                    path: '/person_info',
                    name: '个人信息',
                    component: () => import("../views/main/components/PersonInfo.vue")
                },
            ]
        },
        {
            path: '/:pathMatch(.*)',
            component: () => import("../views/notFound/index.vue")
        }
    ]
})
// 路由导航守卫
router.beforeEach((to) => {
    localStorage.setItem('fullPath', to.fullPath);  // 保存路径到本地
    const token = localStorage.getItem('token');
    if (!token && to.path !== '/login') {    // 还没有登录
        return '/login';
    } else if (token && to.path === '/login') {
        return '/home'
    }
})
export default router;